package com.txby.pf.modules.busi.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.pf.common.validator.group.UpdateGroup;
import com.txby.pf.modules.busi.entity.BusiNewsEntity;
import com.txby.pf.modules.busi.service.BusiNewsService;
import com.txby.pf.modules.sys.controller.AbstractController;

/**
 * 新闻编辑
 */
@RestController
@RequestMapping("/busi/news")
public class BusiNewsController extends AbstractController {
	@Autowired
	private BusiNewsService busiNewsService;

	
	/**
	 * 新闻图片上传
	 */
	@RequestMapping("/fileUpload")
	public R fileUpload(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<BusiNewsEntity> newsList = busiNewsService.queryList(query);
		int total = busiNewsService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(newsList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}
	
	
	
	/**
	 * 新闻列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<BusiNewsEntity> newsList = busiNewsService.queryList(query);
		int total = busiNewsService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(newsList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@RequestMapping("/info/{newsId}")
	public R info(@PathVariable("newsId") Long newsId){
		BusiNewsEntity news = busiNewsService.queryObject(newsId);
		return R.ok().put("news", news);
		
	}
	
	/**
	 * 保存新闻
	 */
	@SysLog("保存新闻")
	@RequestMapping("/save")
	public R save(@RequestParam("title") String title,@RequestParam("subhead") String subhead,@RequestParam("newsType") String newsType,@RequestParam("content") String content){
		BusiNewsEntity news= new BusiNewsEntity();
		news.setTitle(title);
		news.setSubhead(subhead);
		news.setNewsType(newsType);
		news.setContent(content);
		ValidatorUtils.validateEntity(news, AddGroup.class);
		news.setCreater(110L);
		busiNewsService.save(news);
		return R.ok();
	}
	
	/**
	 * 修改新闻
	 */
	@SysLog("修改新闻")
	@RequestMapping("/update")
	public R update(@RequestParam("title") String title,@RequestParam("subhead") String subhead,@RequestParam("newsType") String newsType,
			@RequestParam("content") String content,@RequestParam("newsId") String newsId){
		
		BusiNewsEntity news= new BusiNewsEntity();
		news.setTitle(title);
		news.setSubhead(subhead);
		news.setNewsType(newsType);
		news.setContent(content);
		news.setNewsId(Long.valueOf(newsId));
		ValidatorUtils.validateEntity(news, UpdateGroup.class);
		news.setUpdater(getUserId());
		busiNewsService.update(news);
		return R.ok();
	}
	
	/**
	 * 删除新闻
	 */
	@SysLog("新闻删除")
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] newsIds){
		busiNewsService.deleteBatch(newsIds);
		return R.ok();
	}
}
